package com.supor.aiot.common.helper;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import com.android.baseconfig.base.BaseSharedPreferences;
import com.android.baseconfig.base.RxBus3;
import com.android.baseconfig.common.utils.GetAndroidUniqueMark;
import com.android.baseconfig.common.utils.GsonUtil;
import com.android.baseconfig.common.utils.Logc;
import com.android.baseconfig.common.utils.ThreadUtils;
import com.google.gson.reflect.TypeToken;
import com.supor.aiot.common.app.AppAgent;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.app.api.DeviceImpl;
import com.supor.aiot.common.entity.DeviceInfo;
import com.supor.aiot.common.entity.DeviceTree;
import com.supor.aiot.common.entity.LocationInfo;
import com.supor.aiot.common.entity.ProductType;
import com.supor.aiot.common.entity.RecordBean;
import com.supor.aiot.common.entity.UserInfo;

import java.util.List;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2020, by het, Shenzhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: DataCache <br>
 * 作者: lei <br>
 * 日期: 2020/12/4 <br>
 */
public class GlobalDataCache {
    /**
     * 外网ip
     */
    public static String OUT_IP;
    /**
     * 用户信息
     */
    public static UserInfo userInfo = null;
    /**
     * 安卓设备唯一标识
     */
    public static String uniqueId = null;
    /**
     * 定位信息
     */
    public static LocationInfo locationInfo = null;
    /**
     * 绑定的设备列表
     */
    public static DeviceTree deviceTree = null;
    /**
     * 产品品类列表
     */
    public static List<ProductType> productTypeList = null;
    /**
     * 触碰NFC的参数
     */
    public static String NFC_PARAMS = null;
    /**
     * 触碰NFC的record数据
     */
    public static RecordBean recordBean = null;
    public static boolean ifNfcPop = false;
    /**
     * 主页显示的页面号
     */
    public static int homeIndex = 1;

    /**
     * 获取安卓设备唯一标识
     *
     * @return
     */
    public static String getUniqueId() {
        if (!TextUtils.isEmpty(uniqueId)) {
            return uniqueId;
        }
        uniqueId = BaseSharedPreferences.getString(AppAgent.getApp(), Constants.Key.UNIQUE_ID);
        if (TextUtils.isEmpty(uniqueId)) {
            uniqueId = GetAndroidUniqueMark.getUniqueId(AppAgent.getApp());
            // 增加安卓前缀
            uniqueId = "ANDROID-" + uniqueId;
            BaseSharedPreferences.setString(AppAgent.getApp(), Constants.Key.UNIQUE_ID, uniqueId);
        }
        Logc.d("getUniqueId: " + uniqueId);
        return uniqueId;
    }


    /**
     * 获取用户绑定的设备列表
     */
    @SuppressLint("CheckResult")
    public static void getBindDevice(boolean ifRefresh) {
        if (!AppAgent.isLogin()) {
            return;
        }
        DeviceImpl.getInstance().getBindDevice().subscribe(result -> {
            GlobalDataCache.deviceTree = result.data;
            if (GlobalDataCache.deviceTree != null) {
                WsHelper.getInstance().setDeviceTree(GlobalDataCache.deviceTree);
                // WsHelper.getInstance().init(AppAgent.getApp(), true);
            }
            RxBus3.getInstance().post(Constants.Tag.BINDING_STATUS, Constants.Value.SUCCESS);
        }, throwable -> {
            Logc.e("accept: " + throwable);
            if (ifRefresh) {
                ThreadUtils.execute(GlobalDataCache::getBindDevice, 10 * 1000);
            }
        });
    }

    /**
     * 获取用户绑定的设备列表
     */
    @SuppressLint("CheckResult")
    public static void getBindDevice() {
        getBindDevice(false);
    }

    /**
     * 获取所有品类列表
     */
    @SuppressLint("CheckResult")
    public static void getAllProduct() {
        DeviceImpl.getInstance().getAllProduct().subscribe(result -> {
            List<ProductType> productTypeList = result.data.rows;
            for (ProductType productType : productTypeList) {
                if (productType.Children == null) {
                    continue;
                }
                for (ProductType.ProductModel child : productType.Children) {
                    if (TextUtils.isEmpty(child.modelPicture)) {
                        child.modelPicture = productType.modelPicture;
                    }
                    // pid
                    child.productId = child.salsModelName;
                }
            }
            GlobalDataCache.productTypeList =
                    GsonUtil.getInstance().toObject(GsonUtil.getInstance().toJson(productTypeList), new TypeToken<List<ProductType>>() {
                    }.getType());
        }, throwable -> {
            Logc.e("accept: " + throwable);
        });
    }
}
